package com.botarena.client.contest;

import java.util.Date;
import java.util.List;

import com.botarena.client.RightMenu;
import com.botarena.client.UIHelper;
import com.botarena.client.contest.remote.ContestAccess;
import com.botarena.client.contest.remote.ContestAccessAsync;
import com.botarena.shared.ContestInfo;
import com.botarena.shared.ContestState;
import com.botarena.shared.RankInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Tworzy elementy GUI zwiazane z ContestService
 * 
 * @author wirenth
 * 
 */
public class ContestViewer {

	public static Panel createRank(RightMenu rightMenu) {
		final VerticalPanel panel = new VerticalPanel();
		panel.setWidth("100%");

		if (rightMenu.getSelectedContest() == null) {
			panel.add(new Label("No contest selected"));
		} else {

			final RankingPanel ranking = createRanking(rightMenu, panel);

			panel.add(ranking);
		}

		return panel;
	}

	public static Panel createTopNRank(RightMenu rightMenu) {
		final RankingTop ranking = new RankingTop();
		ranking.setStyleName("contentElementFull");

		ContestInfo contest = rightMenu.getSelectedContest();

		ContestAccessAsync daa = GWT.create(ContestAccess.class);
		daa.getRankTop(contest.getKey(), UIHelper.TOP_N,
				new AsyncCallback<List<RankInfo>>() {

					@Override
					public void onFailure(Throwable caught) {
						ranking.clear();
						ranking
								.add(new Label(
										"Failed to load top N bots info!"));
					}

					@Override
					public void onSuccess(List<RankInfo> result) {
						ranking.setRankingData(result);
					}

				});

		return ranking;
	}

	public static Panel createContestInfoPanel(RightMenu rightMenu) {
		final Panel panel = new VerticalPanel();
		ContestInfo contest = rightMenu.getSelectedContest();
		panel.setStyleName("infoBox");

		ContestAccessAsync daa = GWT.create(ContestAccess.class);

		daa.getContestState(contest.getKey(),
				new AsyncCallback<ContestState>() {

					@Override
					public void onFailure(Throwable caught) {
						panel.clear();
						panel.add(new Label("Failed to load contest info!"));
					}

					@Override
					public void onSuccess(ContestState result) {
						Label contest = new Label("Contest information");
						contest.setStyleName("title2");

						Label name = new Label(result.name);
						name.setStyleName("title3");

						StringBuffer dateStr = new StringBuffer();
						dateStr.append("Open from ").append(
								dateToString(result.from)).append(" to ")
								.append(dateToString(result.to));
						Label date = new Label(dateStr.toString());
						date.setStyleName("title4");

						panel.add(contest);
						panel.add(name);
						panel.add(date);
					}

				});

		return panel;
	}

	private static String dateToString(Date d) {

		DateTimeFormat format = DateTimeFormat.getFormat("dd.MM.yyyy HH:mm");

		return format.format(d);
	}

	private static RankingPanel createRanking(RightMenu rightMenu,
			final VerticalPanel panel) {
		final RankingPanel ranking = new RankingPanel();

		ContestInfo contest = rightMenu.getSelectedContest();

		ContestAccessAsync daa = GWT.create(ContestAccess.class);
		daa.getRanking(contest.getKey(), RankingPanel.PAGE_SIZE,
				new AsyncCallback<List<RankInfo>>() {

					@Override
					public void onFailure(Throwable caught) {
						panel.add(new Label("Can not load ranking"));
						caught.printStackTrace();
					}

					@Override
					public void onSuccess(List<RankInfo> result) {
						System.out.println(result);
						ranking.setRankingData(result);
					}

				});
		return ranking;
	}

}
